//package yh.thread.config;
//
//import com.alibaba.druid.filter.Filter;
//import com.alibaba.druid.filter.stat.StatFilter;
//import com.alibaba.druid.pool.DruidDataSource;
//import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
//import com.alibaba.druid.support.http.StatViewServlet;
//import com.alibaba.druid.support.http.WebStatFilter;
//import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
//import com.alibaba.druid.wall.WallConfig;
//import com.alibaba.druid.wall.WallFilter;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.boot.web.servlet.MultipartConfigFactory;
//import org.springframework.boot.web.servlet.ServletRegistrationBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.util.unit.DataSize;
//
//import javax.servlet.MultipartConfigElement;
//import javax.sql.DataSource;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// *  Druid连接池配置
// *
// * Druid连接池本身不处理事务，事务是通过Spring的事务管理器来控制的，比如DataSourceTransactionManager。
// * 如果你需要在Spring Boot项目中使用Druid连接池并结合Spring的事务管理，
// * 你需要额外配置@EnableTransactionManagement注解和@Transactional注解，以及相应的事务管理器Bean。
// *
// * @author: zyh
// * @date: 2024/12/12 19:32
// **/
//@Configuration
//public class DruidConfig {
//
//    private final Logger logger = LoggerFactory.getLogger(DruidConfig.class);
//
//    @Value("${spring.datasource.url:#{null}}")
//    private String dbUrl;
//    @Value("${spring.datasource.username: #{null}}")
//    private String username;
//    @Value("${spring.datasource.password:#{null}}")
//    private String password;
//    @Value("${spring.datasource.driver-class-name:#{null}}")
//    private String driverClassName;
//    @Value("${spring.datasource.druid.initial-size:#{null}}")
//    private Integer initialSize;
//    @Value("${spring.datasource.druid.min-idle:#{null}}")
//    private Integer minIdle;
//    @Value("${spring.datasource.druid.max-active:#{null}}")
//    private Integer maxActive;
//    @Value("${spring.datasource.druid.max-wait:#{null}}")
//    private Integer maxWait;
//    @Value("${spring.datasource.time-between-eviction-runs-millis:#{null}}")
//    private Integer timeBetweenEvictionRunsMillis;
//    @Value("${spring.datasource.min-evictable-idle-time-millis:#{null}}")
//    private Integer minEvictableIdleTimeMillis;
//    @Value("${spring.datasource.validation-query:#{null}}")
//    private String validationQuery;
//    @Value("${spring.datasource.test-while-idle:#{null}}")
//    private Boolean testWhileIdle;
//    @Value("${spring.datasource.test-on-borrow:#{null}}")
//    private Boolean testOnBorrow;
//    @Value("${spring.datasource.test-on-return:#{null}}")
//    private Boolean testOnReturn;
//    @Value("${spring.datasource.pool-prepared-statements:#{null}}")
//    private Boolean poolPreparedStatements;
//    @Value("${spring.datasource.max-pool-prepared-statement-per-connection-size:#{null}}")
//    private Integer maxPoolPreparedStatementPerConnectionSize;
//    @Value("${spring.datasource.filters:#{null}}")
//    private String filters;
//    @Value("{spring.datasource.connectionProperties:#{null}}")
//    private String connectionProperties;
//
//
//
//    @Bean
//    @Primary
//    public DataSource dataSource(){
//        DruidDataSource datasource = new DruidDataSource();
//
//        datasource.setUrl(this.dbUrl);
//        datasource.setUsername(username);
//        datasource.setPassword(password);
//        datasource.setDriverClassName(driverClassName);
//        //configuration
//        if(initialSize != null) {
//            datasource.setInitialSize(initialSize);
//        }
//        if(minIdle != null) {
//            datasource.setMinIdle(minIdle);
//        }
//        if(maxActive != null) {
//            datasource.setMaxActive(maxActive);
//        }
//        if(maxWait != null) {
//            datasource.setMaxWait(maxWait);
//        }
//        if(timeBetweenEvictionRunsMillis != null) {
//            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        }
//        if(minEvictableIdleTimeMillis != null) {
//            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//        }
//        if(validationQuery!=null) {
//            datasource.setValidationQuery(validationQuery);
//        }
//        if(testWhileIdle != null) {
//            datasource.setTestWhileIdle(testWhileIdle);
//        }
//        if(testOnBorrow != null) {
//            datasource.setTestOnBorrow(testOnBorrow);
//        }
//        if(testOnReturn != null) {
//            datasource.setTestOnReturn(testOnReturn);
//        }
//        if(poolPreparedStatements != null) {
//            datasource.setPoolPreparedStatements(poolPreparedStatements);
//        }
//        if(maxPoolPreparedStatementPerConnectionSize != null) {
//            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//        }
//
//        if(connectionProperties != null) {
//            datasource.setConnectionProperties(connectionProperties);
//        }
//
//        List<Filter> filters = new ArrayList<>();
//        filters.add(statFilter());
//        filters.add(wallFilter());
//        datasource.setProxyFilters(filters);
//
//        return datasource;
//    }
//
//
//    @Bean
//    public ServletRegistrationBean druidServlet() {
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean( new StatViewServlet(), "/druid/*");
//        //控制台管理用户，加入下面2行 进入druid后台就需要登录
//        servletRegistrationBean.addInitParameter("loginUsername", "admin");
//        servletRegistrationBean.addInitParameter("loginPassword", "admin");
//        return servletRegistrationBean;
//    }
//
//    // 配置 Druid 的 Spring 监控拦截器
//    @Bean
//    public DruidStatInterceptor druidStatInterceptor() {
//        return new DruidStatInterceptor();
//    }
//
//    // 配置 Bean 的自动代理
//    @Bean
//    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
//        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
//        creator.setInterceptorNames("druidStatInterceptor");
//        creator.setBeanNames("*Service", "*Controller");
//        creator.setProxyTargetClass(true);
//        return creator;
//    }
//
//
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(new WebStatFilter());
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        filterRegistrationBean.addInitParameter("profileEnable", "true");
//        return filterRegistrationBean;
//    }
//
//
//    @Bean
//    public StatFilter statFilter(){
//        StatFilter statFilter = new StatFilter();
//        statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准，执行时间超过slowSqlMillis的就是慢。
//        statFilter.setMergeSql(true); //SQL合并配置
//        statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000，也就是3秒。
//        return statFilter;
//    }
//
//    @Bean
//    public WallFilter wallFilter(){
//        WallFilter wallFilter = new WallFilter();
//        //允许执行多条SQL
//        WallConfig config = new WallConfig();
//        config.setMultiStatementAllow(true);
//        wallFilter.setConfig(config);
//        return wallFilter;
//    }
//
//
//    @Bean
//    public MultipartConfigElement multipartConfigElement() {
//
//        MultipartConfigFactory factory = new MultipartConfigFactory();
//
//        //// 设置文件大小限制 ,超了，页面会抛出异常信息，这时候就需要进行异常信息的处理了;
//
//        factory.setMaxFileSize(DataSize.ofMegabytes(100L)); //KB,MB
//
//        /// 设置总上传数据总大小
//
//        factory.setMaxRequestSize(DataSize.ofMegabytes(100L));
//
//        return factory.createMultipartConfig();
//
//    }
//
//
//}
